package belle.com.myapp.view.home.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import belle.com.myapp.R;
import belle.com.myapp.base.BaseFragment;
import belle.com.myapp.manager.MyLinearLayoutManager;
import belle.com.myapp.retrofit.ApiService;
import belle.com.myapp.view.board.activity.LeaderBoardActivity;
import belle.com.myapp.view.fm.activity.EveryActivity;
import belle.com.myapp.view.fm.activity.PersonalFmActivity;
import belle.com.myapp.view.home.model.BannerResponse;
import belle.com.myapp.view.home.model.GlideImageLoader;
import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Title :推荐
 * Description :
 * Author:  Belle Guan    date : 2018/5/31 14:58
 * Update :                date :
 * Version : 1.0.0
 * Copyright : copyright(c) 浙江亚厦股份有限公司 2017 ~ 2020 版权所有
 *
 * @author guanjiali1
 */
public class SongFragment extends BaseFragment {

    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.personal_fm)
    LinearLayout personal_fm;
    @BindView(R.id.every_recommend)
    LinearLayout every_recommend;
    @BindView(R.id.song_list)
    LinearLayout song_list;
    @BindView(R.id.leader_board)
    LinearLayout leader_board;
    List<String> listImg = new ArrayList<>();

    private RecyclerView mRecyclerView;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_song;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        MyLinearLayoutManager.initItemButton(personal_fm, "私人FM", R.drawable.personal_fm, PersonalFmActivity.class, getContext());
        MyLinearLayoutManager.initItemButton(every_recommend, "每日推荐", R.drawable.actionbar_disco, EveryActivity.class, getContext());
        MyLinearLayoutManager.initItemButton(song_list, "歌单", R.drawable.actionbar_disco);
        MyLinearLayoutManager.initItemButton(leader_board, "排行榜", R.drawable.actionbar_disco, LeaderBoardActivity.class, getContext());
        queryDate();
        setRecycleView();
    }


    @Override
    protected boolean isNeedEventBus() {
        return false;
    }

    @Override
    protected View initViews(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return null;
    }

    private void setRecycleView() {
    }

    private void queryDate() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://10.20.30.216:4000/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiService service = retrofit.create(ApiService.class);
        Call<BannerResponse> call = service.banner();
        call.enqueue(new Callback<BannerResponse>() {
            @Override
            public void onResponse(Call<BannerResponse> call, Response<BannerResponse> response) {
                System.out.println("返回值：" + response.body());
                initBanner(response);
            }

            @Override
            public void onFailure(Call<BannerResponse> call, Throwable t) {
                System.out.println(t);
            }
        });
    }

    private void initBanner(Response<BannerResponse> response) {

        for (BannerResponse.BannersBean bannersBean : response.body().getBanners()) {
            listImg.add(bannersBean.getPicUrl());
        }
        banner.setImages(listImg)
                .setImageLoader(new GlideImageLoader())
                .setIndicatorGravity(BannerConfig.RIGHT)
                .start();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setDefaultFragmentTitle(String title) {

    }

}
